package com.crime.crimemonitorclient;

import java.util.Calendar;
import java.util.Date;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.DatePicker;

public class SelectCrimeDateIntervalDialogFragment extends DialogFragment {
	
	private SelectCrimeDateIntervalDialogListener mListener;
	private long mMinDateInMillis;
	private long mMaxDateInMillis;
	private long mSelectedStartDate;
	private long mSelectedEndDate;
	
	public SelectCrimeDateIntervalDialogFragment(long minDate, long maxDate, long selectedStartDate, long selectedEndDate)
	{
		if(minDate > maxDate)
			throw new IllegalArgumentException("minDate cannot be greater than maxDate");
		
		mMinDateInMillis = minDate;
		mMaxDateInMillis = maxDate;
		mSelectedStartDate = selectedStartDate;
		mSelectedEndDate = selectedEndDate;
	}
	
	public interface SelectCrimeDateIntervalDialogListener
	{
		public void onCrimeDateIntervalSelected(long startDateInMillis, long endDateInMillis);
	}
	
	@Override
	public void onAttach(Activity activity)
	{
		super.onAttach(activity);
		
		try
		{
			mListener = (SelectCrimeDateIntervalDialogListener)activity;
		}
		catch(ClassCastException e)
		{
			throw new ClassCastException("Activity must implement SelectCrimeDateIntervalDialogListener");
		}
	}
	
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the Builder class for convenient dialog construction
    	LayoutInflater inflater = this.getActivity().getLayoutInflater();
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        
        View dialogView = inflater.inflate(R.layout.select_date_interval_dialog, null);
        final DatePicker startDatePicker = (DatePicker)dialogView.findViewById(R.id.select_date_interval_startDate);
        Date startDate = new Date(mSelectedStartDate);
        startDatePicker.setCalendarViewShown(false);
        startDatePicker.setMinDate(mMinDateInMillis);
        startDatePicker.setMaxDate(mMaxDateInMillis);
        startDatePicker.updateDate(startDate.getYear(), startDate.getMonth(), startDate.getDay());
        
        final DatePicker endDatePicker = (DatePicker)dialogView.findViewById(R.id.select_date_interval_endDate);
        Date endDate = new Date(mSelectedEndDate);
        endDatePicker.setCalendarViewShown(false);
        endDatePicker.setMinDate(mMinDateInMillis);
        endDatePicker.setMaxDate(mMaxDateInMillis);
        startDatePicker.updateDate(endDate.getYear(), endDate.getMonth(), endDate.getDay());
        
        builder
        	.setView(dialogView)
        	.setTitle(R.string.display_crime_detail_set_interval)
        	.setPositiveButton("OK", new OnClickListener()
			{
				@Override
				public void onClick(DialogInterface dialog, int which)
				{
					// TODO: kontrola, ze je datum "do" vetsi nebo rovno nez datum "od"
					long selectedStartDateInMillis, selectedEndDateInMillis;
					
					Calendar cal = Calendar.getInstance();
					cal.clear();
					cal.set(Calendar.YEAR, startDatePicker.getYear());
					cal.set(Calendar.MONTH, startDatePicker.getMonth());
					cal.set(Calendar.DAY_OF_MONTH, startDatePicker.getDayOfMonth());
					
					selectedStartDateInMillis = cal.getTimeInMillis();
					
					cal.clear();
					cal.set(Calendar.YEAR, endDatePicker.getYear());
					cal.set(Calendar.MONTH, endDatePicker.getMonth());
					cal.set(Calendar.DAY_OF_MONTH, endDatePicker.getDayOfMonth());
					
					selectedEndDateInMillis = cal.getTimeInMillis();
					
					// uzivatel vybral interval, je nutne notifikovat aktivitu
					mListener.onCrimeDateIntervalSelected(selectedStartDateInMillis, selectedEndDateInMillis);
				}
			})
			.setNegativeButton("Cancel", new OnClickListener()
			{
				@Override
				public void onClick(DialogInterface dialog, int which)
				{
					SelectCrimeDateIntervalDialogFragment.this.getDialog().cancel();
				}
			});
        // Create the AlertDialog object and return it
        return builder.create();
    }
}
